package com.rzn.reading.controller;


import com.rzn.reading.BusinessException;
import com.rzn.reading.JsonResult;
import com.rzn.reading.Message;
import com.rzn.reading.dto.PageableRequestDTO;
import com.rzn.reading.model.dto.literature.*;
import com.rzn.reading.service.LiteraturedetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 文献详细信息表 前端控制器
 * </p>
 *
 * @author rzn
 * @since 2022-02-12
 */
@RestController
@RequestMapping("//literaturedetail")
@Api(tags = "文献信息")
public class LiteraturedetailController {

    private final LiteraturedetailService literaturedetailService;

    public LiteraturedetailController(LiteraturedetailService literaturedetailService) {
        this.literaturedetailService = literaturedetailService;
    }

    @DeleteMapping("/delete/{userId}/{literatureId}")
    @ApiOperation("删除文献")
    public JsonResult deleteLiterature(@PathVariable Integer userId,@PathVariable Integer literatureId){
        Boolean res = literaturedetailService.deleteLiterature(userId,literatureId);
        if (!res) {
            throw new BusinessException(new Message("DB.DELETE_FAILED", "文献").toString());
        }
        return JsonResult.success(new Message("DB.DELETE_SUCCESS", "文件").toString());
    }

    @PostMapping("/upload")
    @ApiOperation("添加文献")
    public JsonResult uploadLiterature(@RequestBody LiteratureCreateInputDTO literatureCreateInputDTO){
        Boolean res = literaturedetailService.addLiterature(literatureCreateInputDTO);
        if (!res) {
            throw new BusinessException(new Message("DB.ADD_FAILED", "文件").toString());
        }
        return JsonResult.success(new Message("DB.ADD_SUCCESS", "文件").toString());
    }

    @PostMapping("/queryList")
    @ApiOperation("分页查询文献列表")
    public JsonResult queryLiteratureList(@RequestBody LiteratureQueryDTO literatureQueryDTO){
        List<LiteratureItemListDTO> res = literaturedetailService.getLiteratureItemList(literatureQueryDTO);
        return JsonResult.success(res);
    }

    @GetMapping("/allCount")
    @ApiOperation("查询文献总数目")
    public JsonResult allLiteratureCount(){
        Integer allCount = literaturedetailService.getAllCount();
        return JsonResult.success(allCount);
    }

    @GetMapping("/queryLiteratureGrade/{userId}")
    @ApiOperation("上传者查询自己上传的文献，显示评分")
    public JsonResult queryDifferentType(@PathVariable Integer userId){
        List<LiteratureTypeGradeDTO> literatureTypeGrade = literaturedetailService.getLiteratureTypeGrade(userId);
        return JsonResult.success(literatureTypeGrade);
    }

    @GetMapping("/queryLiteratureDetail/{userId}/{literatureId}")
    @ApiOperation("查询文献阅读详情")
    public JsonResult getLiteratureDetail(@PathVariable Integer userId,@PathVariable Integer literatureId){
        LiteratureDetailDTO literatureDetail = literaturedetailService.getLiteratureDetail(userId, literatureId);
        return JsonResult.success(literatureDetail);
    }
}

